package com.course.masterservice.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.course.component.http.BasePageResponse;
import com.course.component.http.requ.RequestCategory;
import com.course.component.util.CopyUtil;
import com.course.component.util.DateUtils;
import com.course.component.util.SnowFlake;
import com.course.entity.Category;
import com.course.masterservice.CategoryMasterService;
import com.course.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @Copyright: Copyright (c)  林中小鸟  All rights reserved.
 * @author: moon
 * @date: 2022/9/17 11:09
 * @Description: **********************
 */
@Service
public class CategoryMasterServiceImpl implements CategoryMasterService {

    @Autowired
    private CategoryService categoryService;


    @Override
    public BasePageResponse<Category> getPageList(RequestCategory requestCategory) {
        //使用分页
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        //搜索逻辑

        /*
        //MyBatis 分页插件 PageHelper 插件使用
        PageHelper.startPage(requestCategory.getPageNum().intValue(), requestCategory.getPageSize().intValue());
        List<Category> list = categoryService.list(queryWrapper);
        PageInfo<Category> pageinfo = new PageInfo<>(list);
        return new BasePageResponse(pageinfo.getTotal(),list);
        */
        IPage<Category> list = categoryService.page(new Page<>(requestCategory.getPage().intValue(),requestCategory.getPageSize().intValue()),queryWrapper);
        return new BasePageResponse(list.getTotal(),list.getRecords());
    }

    @Override
    public Boolean del(RequestCategory requestCategory) {
        return categoryService.removeById(requestCategory.getId());
    }

    @Override
    public Boolean update(RequestCategory requestCategory) {
        Category updateData = CopyUtil.copy(requestCategory,Category.class);
        updateData.setUpdateTime(DateUtils.getTime());
        if(ObjectUtils.isEmpty(updateData.getId())){
            updateData.setId(new SnowFlake().nextId());
            updateData.setCreateTime(DateUtils.getTime());
        }
        return categoryService.saveOrUpdate(updateData);
    }
}
